package com.onec.service.netty.channelpipeline;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.embedded.EmbeddedChannel;
import java.nio.Buffer;
import org.junit.Test;

public class InPipeLine {

  static class SimpleHandleA extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
      System.out.println("A : 被调用");
      super.channelRead(ctx, msg);
    }
  }

  static class SimpleHandleB extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
      System.out.println("B : 被调用");
      super.channelRead(ctx, msg);
    }
  }

  static class SimpleHandleC extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
      System.out.println("C : 被调用");
      super.channelRead(ctx, msg);
    }
  }

  @Test
  public void testPipeline() {
    ChannelInitializer i = new ChannelInitializer<EmbeddedChannel>() {
      @Override
      protected void initChannel(EmbeddedChannel ch) throws Exception {
        ch.pipeline().addLast(new SimpleHandleA());
        ch.pipeline().addLast(new SimpleHandleB());
        ch.pipeline().addLast(new SimpleHandleC());

      }
    };

    EmbeddedChannel embeddedChannel = new EmbeddedChannel(i);

    ByteBuf byteBuf = Unpooled.buffer();
    byteBuf.writeInt(2);

    embeddedChannel.writeInbound(byteBuf);

    embeddedChannel.flush();
    embeddedChannel.close();
  }

}


